import java.util.HashMap;
import java.util.Map;

public class Leetcode1797 {
    public static void main(String[] args) {
        AuthenticationManager authenticationManager = new AuthenticationManager(5);
        authenticationManager.renew("aaa", 1);
        authenticationManager.generate("aaa", 2);
        System.out.println(authenticationManager.countUnexpiredTokens(6));
        authenticationManager.generate("bbb", 7);
        authenticationManager.renew("aaa", 8);
        authenticationManager.renew("bbb", 10);
        System.out.println(authenticationManager.countUnexpiredTokens(15));
    }

    static class AuthenticationManager {
        Map<String, Integer> map;
        int timeToLive;

        public AuthenticationManager(int timeToLive) {
            map = new HashMap<>();
            this.timeToLive = timeToLive;
        }

        public void generate(String tokenId, int currentTime) {
            map.put(tokenId, currentTime + timeToLive);
        }

        public void renew(String tokenId, int currentTime) {
            Integer timeout = map.get(tokenId);
            if (timeout == null) {
                return;
            }

            if (timeout <= currentTime) {
                map.remove(tokenId);
                return;
            }

            map.put(tokenId, currentTime + timeToLive);
        }

        public int countUnexpiredTokens(int currentTime) {
            return (int) map.values().stream().filter(timeOut -> timeOut > currentTime).count();
        }
    }
}
